//
//  LWPopController.swift
//  shop
//
//  Created by 刘伟 on 2017/3/3.
//
//

import UIKit

enum LWAnimateType {
    case first
    case second
}

enum LWViewType {
    case maskView
    case popView
}

enum LWActionType {
    case popUp
    case popDown
}

class LWPopController: UIViewController {

    // 主视图控制器——视图
    var rootView = UIView()
    // 遮挡层
    var maskView: UIView!
    // 弹出层视图
    var popView:UIView!
    
    // 弹出层的高度 默认：400
    var popViewHeight:CGFloat = SCREEN_HEIGHT * 4 / 5
    
    // 周期
    var duration: TimeInterval = 0.3
    
    override func viewDidLoad() {
        super.viewDidLoad()
        if let selfNV = self.navigationController {
            rootView = selfNV.view
        } else {
            rootView = self.view
        }
        maskView = UIView(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT))
        maskView.backgroundColor = UIColor.black
        maskView.alpha = 0.2
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(popDown))
        maskView.addGestureRecognizer(tapGesture)
        popView = UIView(frame: CGRect(x: 0, y: SCREEN_HEIGHT, width: SCREEN_WIDTH, height: popViewHeight))
        /// popView1 是点击打开的时候下方弹出的view
        popView.backgroundColor = UIColor.white
        /// 加个阴影
        popView.layer.shadowColor = UIColor.black.cgColor
        popView.layer.shadowOffset = CGSize(width: 0.5, height: 0.5)
        popView.layer.shadowOpacity = 0.8
        popView.layer.shadowRadius = 5
        popView.isUserInteractionEnabled = false
    }
    
    // 弹出视图操作
    func popUp() {
        UIApplication.shared.keyWindow?.addSubview(maskView)
        UIApplication.shared.keyWindow?.addSubview(popView)
        let popViewFrame = getViewFrame(for: .popView, type: .popUp)
        UIView.animate(withDuration: self.duration,
                       delay: 0,
                       options: UIViewAnimationOptions.curveEaseInOut,
                       animations: {
                        self.rootView.layer.transform = self.transformAnimation(type: .first)
        }) { (bool) in
            UIView.animate(withDuration: self.duration, delay: 0, options: UIViewAnimationOptions.curveEaseInOut, animations: {
                self.rootView.layer.transform = self.transformAnimation(type: .second)
                self.popView.frame = popViewFrame
            }, completion: {(bool) in
            })
        }
        popView.isUserInteractionEnabled = true
    }
    
    // 弹回操作
    func popDown() {
        self.maskView.removeFromSuperview()
        let popViewFrame = getViewFrame(for: .popView, type: .popDown)
        popView.isUserInteractionEnabled = false
        UIView.animate(withDuration: self.duration,
                       delay: 0,
                       options: UIViewAnimationOptions.curveEaseInOut,
                       animations: {
                        self.rootView.layer.transform = self.transformAnimation(type: .first)
        }) { (bool) in
            UIView.animate(withDuration: self.duration, delay: 0, options: UIViewAnimationOptions.curveEaseInOut, animations: {
                self.popView.frame = popViewFrame
                self.rootView.layer.transform = CATransform3DIdentity
            }, completion: {(bool) in
                self.popView.removeFromSuperview()
            })
        }
    }
    
    // 动画效果
    fileprivate func transformAnimation(type: LWAnimateType) -> CATransform3D {
        var transform = CATransform3DIdentity
        switch type {
        case .first:
            // 视图角度
            transform.m34 = -1.0 / 2000;
            // 尺寸缩小(transform对象,X轴,Y轴,Z轴)
            transform = CATransform3DScale(transform, 1, 1, 1)
            // 沿某轴旋转(transform对象,旋转角度,X轴,Y轴,Z轴)
            let angel = 15.0 * (CGFloat)(Double.pi) / 180.0
            transform = CATransform3DRotate(transform, angel, 1, 0, 0)
        case .second:
            // 第二次 变形实在第一次的基础上
            transform.m34 = transformAnimation(type: .first).m34
            // 沿着某轴移动(transform对象，X轴,Y轴,Z轴)
            transform = CATransform3DTranslate(transform, 0, 10, 0)
            // 尺寸缩小(transform对象,X轴,Y轴,Z轴)
            transform = CATransform3DScale(transform, (SCREEN_WIDTH-40)/SCREEN_WIDTH, (SCREEN_HEIGHT-20)/SCREEN_HEIGHT, 1)
        }
        return transform
    }
    
    
    
    // 获取 popView 和 maskView 新的 frame
    func getViewFrame(for forView: LWViewType, type: LWActionType) -> CGRect {
        var frame:CGRect
        switch forView {
        case .maskView:
            switch type {
            case .popUp:
                // maskView 出现时的 frame
                frame = maskView.frame
                frame = CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT)
            case .popDown:
                // maskView 隐藏时的 View
                frame = maskView.frame
                frame = CGRect(x: SCREEN_WIDTH / 2, y: SCREEN_HEIGHT / 2, width: 0, height: 0)
            }
        case .popView:
            switch type {
            case .popUp:
                // popView 出现时的 frame
                frame = popView.frame
                frame.origin.y = SCREEN_HEIGHT - popViewHeight
            case .popDown:
                // popView 隐藏时的 frame
                frame = popView.frame
                frame.origin.y = SCREEN_HEIGHT
            }
        }
        return frame
    }
    
    
    
    
    
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}
